#! /bin/bash

if [ $# != 2 ] || [ $1 == "-h" ] || [ $1 == "-help" ] ; then 

  echo '使用方式: ./start_stop.sh start[stop] earth ==> 启动［关闭］earth项目'
  echo '使用方式: ./start_stop.sh start[stop] deduct==> 启动［关闭］bridgewater-deduct项目'
  echo '使用方式: ./start_stop.sh start[stop] remittance ==> 启动［关闭］bridgewater-remittance项目'
  echo '使用方式: ./start_stop.sh start[stop] swissre ==> 启动［关闭］Swissre项目'
  echo '使用方式: ./start_stop.sh start[stop] munichre ==> 启动［关闭］Munichre项目'
  echo '使用方式: ./start_stop.sh start[stop] deloitte ==> 启动［关闭］deloitte项目'
  echo '使用方式: ./start_stop.sh start[stop] morganStanley ==> 启动［关闭］morganStanley项目'
  echo '使用方式: ./start_stop.sh start[stop] citigroup ==> 启动［关闭］citigroup项目'
  echo '使用方式: ./start_stop.sh start[stop] jpmorgan ==> 启动［关闭］jpmorgan项目'
  echo '使用方式: ./start_stop.sh start[stop] barclays ==> 启动［关闭］barclays项目'
  echo '使用方式: ./start_stop.sh start[stop] sign ==> 启动［关闭］sign项目'

exit
	 
fi

project=$2
flag=$1


if [  $flag == "stop" ]; then
  msgStart="准备停止项目"$project
  msgEnd="成功停止项目"$project
else
  msgStart="准备启动项目"$project
  msgEnd="成功启动项目"$project
fi

if [ $project == "earth" ] || [ $project == "deduct" ] || [ $project == "remittance" ] || [ $project == "jpmorgan" ] || [ $project == "barclays" ] ; then
 
   echo $msgStart

  if [ $flag == "stop" ]; then

     ps -ef | grep "tomcat-"$project | grep -v "grep" | grep -v "deploy.sh"  | grep -v "start_stop.sh"  | awk '{print $2}' | xargs kill -9

  else
     
   
     TOMCAT_HOME=/data/webapps/tomcat-$project
    
    $TOMCAT_HOME/bin/startup.sh
  
    sh /data/scripts/myscripts/task.sh $project 

    sh /data/scripts/myscripts/tail_log.sh $project
 fi
  
  echo $msgEnd

fi

if [ $project == "sign" ] || [ $project == "swissre" ] || [ $project == "munichre" ] || [ $project == "deloitte"  ] ||   [ $project == "citigroup" ] || [ $project == "morganStanley" ]; then

  echo $msgStart;

  if [  $flag == "stop" ] ; then
      ps -ef | grep $project | grep -v "grep" | grep -v "start_stop.sh" | grep -v "deploy.sh" | awk '{print $2}' | xargs  kill -9 
#     /etc/init.d/$project stop
  else
    if [ $project == "sign" ] || [ $project == "morganStanley" ] || [ $project == "citigroup" ] || [ $project == "swissre" ]; then
      nohup java -Xms256m -Xmx512m -jar /data/webapps/$project/$project-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod --spring.config.location=/data/scripts/config/$project/ --security.basic.enabled=false --management.security.enabled=false > /dev/null  2>&1 >>  /data/webapps/$project/$project.log &
     
      sh /data/scripts/myscripts/task.sh $project 

      timeout 5 tail -f /data/webapps/$project/$project".log"
    else
      /etc/init.d/$project start --spring.profiles.active=prod --spring.config.location=/data/scripts/config/$project/
      #nohup java -Xms256m -Xmx512m -jar /data/webapps/$project/$project-1.0.0.jar --spring.profiles.active=prod --spring.config.location=/data/scripts/config/$project/ --security.basic.enabled=false --management.security.enabled=false > /dev/null  2>&1 >>  /data/webapps/$project/$project.log & 
      timeout 5 tail -f /data/webapps/$project/logs/$project".log"
    fi
  fi

  echo $msgEnd
fi

if [ $project == "consumer" ] || [ $project == "producer" ] ; then

  echo $msgStart
if [ $project == "consumer"  ]; then

     id_rsa_suffix="207"
     ip="192.168.0.207"

  else
     id_rsa_suffix="206"
     ip="192.168.0.206"
  fi


  if [ $flag == "stop"  ]; then
	ssh -i ~/.ssh/id_rsa_60.190.243.68_22  root@$ip "sh /opt/stop.sh"
  else
        echo "start $ip"
	ssh -i ~/.ssh/id_rsa_60.190.243.68_22  root@$ip "sh /opt/deploy.sh"

  fi
  echo $msgEnd  
fi
